package com.fearless.crm.settings.controller;

import com.fearless.crm.exception.LoginException;
import com.fearless.crm.utils.MD5Util;
import com.fearless.crm.settings.entity.User;
import com.fearless.crm.settings.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService service;

    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> doLoginController(User user, HttpServletRequest request)throws LoginException
    {
        System.out.println("处理器方法开始执行");

        String loginAct=user.getLoginAct();
        String loginPwd=user.getLoginPwd();
        System.out.println(loginAct+" "+loginPwd);
        //将密码的明文形式转为密文形式
        loginPwd= MD5Util.getMD5(loginPwd);

        //接收ip地址
        String ip=request.getRemoteAddr();
        System.out.println("ip地址====="+ip);
        Map<String,Object> map = new HashMap<>();


        User resultUser=service.loginService(loginAct,loginPwd,ip);

        map.put("success",true);
        request.getSession().setAttribute("user",resultUser);
        //如果程序执行到这里，说明业务层(service)没有给控制层抛出异常
        //表示登录成功


        return map;



    }

}
